//
//  VehicleInfo.m
//  bmwTest
//
//  Created by Neutrino on 11-5-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "VehicleInfo.h"

static int TOTAL_IMAGE = 0;
static int filenameIter;
static NSString* imageStoragePath = @"/Users/neutrino/CocoaSourceCode/bmwTest/test/";
static NSString* backgroundImage = @"/Users/neutrino/CocoaSourceCode/bmwTest/test/BMW.jpg";

@implementation VehicleInfo
@synthesize fileName,image;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //UIImageView* pic = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:backgroundImage]];

        //[pic release];
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
 // [[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:backgroundImage]]];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
}
//---------
-(IBAction)start
{
    [self getImagePath:imageStoragePath];
    NSMutableString* imgPath = [[NSMutableString alloc] init];
    [imgPath appendString:imageStoragePath];
    [imgPath appendString:[fileName objectAtIndex:0]];
    UIImage* pic = [UIImage imageWithContentsOfFile:imgPath];
    [image setImage:pic];
    TOTAL_IMAGE = [fileName count];
    filenameIter = 0;
 //   [pic release];
    [imgPath release];
}
-(IBAction)prevImg
{   
    NSMutableString* imgPath = [[NSMutableString alloc] init];
    [imgPath appendString:imageStoragePath];
    if(TOTAL_IMAGE == 0)
        return;
    else if( filenameIter == 0)
    {
        [imgPath appendString:[fileName lastObject]];
        filenameIter = TOTAL_IMAGE-1;
    }
    else
    {
        filenameIter--;
        [imgPath appendString:[fileName objectAtIndex:filenameIter]];
    }
    UIImage* pic = [UIImage imageWithContentsOfFile:imgPath];
    [image setImage:pic];
    [imgPath release];
   
} 
-(IBAction)nextImg
{
    NSMutableString* imgPath = [[NSMutableString alloc] init];
    [imgPath appendString:imageStoragePath];
    if(TOTAL_IMAGE == 0)
        return;
    else if( filenameIter == TOTAL_IMAGE-1)
    {
        [imgPath appendString:[fileName objectAtIndex:0]];
        filenameIter = 0;
    }
    else
    {
        filenameIter++;
        [imgPath appendString:[fileName objectAtIndex:filenameIter]];
    }
    UIImage* pic = [UIImage imageWithContentsOfFile:imgPath];
    [image setImage:pic];
    [imgPath release];

}

-(void)getImagePath:(NSString *)path
{
    NSFileManager* fp = [NSFileManager defaultManager];
    NSString* home = [path stringByExpandingTildeInPath];
    fileName = [[NSMutableArray alloc] init];
    for(NSString* s in [fp enumeratorAtPath:home])
    {
        if([[s pathExtension] isEqualToString:@"jpg"] || [[s pathExtension] isEqualToString:@"jpeg"])
            [fileName addObject:s]; 
    }
}
//------------
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
	return YES;
}

@end
